<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends BaseController {
   public function index()
   {
       $this->display();

   }

    public function upload_pic()
    {
        $path = $_FILES['pic']['tmp_name'];
//        var_dump($path);
        $this->upload_curl_pic($path);
    }

    public function qiniu()
    {
        $model = M('images');
        $setting=C('UPLOAD_SITEIMG_QINIU');
        $Upload = new \Think\Upload($setting);
        $info = $Upload->upload($_FILES);
        $url = $info['file']['url'];
        $image = $this->imageinfo($url);
        $data = array(
            'user_id'=>'',
            'pic_url'=>$url,
            'width'=>$image['width'],
            'height'=>$image['height'],
            'type'=>1,
            'up_time'=>date('Y-m-d H:i:s',time()),
        );
        $model->data($data)->add();
    }

    public function logo()
    {
        $path = $_FILES['file']['tmp_name'];
        $image = new \Think\Image();
// 在图片左上角添加水印（水印文件位于./logo.png） 并保存为water.jpg
        $image->open($path)->water('./Public/images/upload/logo.png',\Think\Image::IMAGE_WATER_SOUTHEAST)->save("./Public/images/upload/water.jpg");
        $img = './Public/images/upload/water.jpg';
        $this->upload_curl_pic2($img);

    }

    function upload_curl_pic($file)
    {
        $url  = 'http://127.0.0.1/svnwelove/index.php/index/logo';
        $fields['file'] = '@'.$file;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        curl_close($ch);
        return $result;
    }

    function upload_curl_pic2($file)
    {
        $url  = 'http://127.0.0.1/svnwelove/index.php/index/qiniu';
        $fields['file'] = '@'.$file;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        curl_close($ch);
        return $result;
    }



    function getImageInfo($img) //$img为图像文件绝对路径
    {
        $img_info = getimagesize($img);
        var_dump($img_info);

//        switch ($img_info[2]) {
//            case 1:
//                $imgtype = "GIF";
//                break;
//            case 2:
//                $imgtype = "JPG";
//                break;
//            case 3:
//                $imgtype = "PNG";
//                break;
//        }
//        $img_type = $imgtype . "图像";
//        $img_size = ceil(filesize($img) / 1000) . "k"; //获取文件大小
//
//        $new_img_info = array(
//            "width" => $img_info[0],
//            "height" => $img_info[1],
//            "type" => $img_type,
//            "size" => $img_size
//        );
//        print " width";
//        print $img_info[0];
//        print " height";
//        print $img_info[1];
//        print " format";
//        print $img_type;
//        print " size";
//        print $img_size;
//        print $new_img_info;
    }




    public function water(){
        header("content-type:image/jpg");
        import('ORG.Util.Image');
        $Image = new Image();
        $Image->water('./public/upload/b.jpg','./public/upload/2.jpg',1);
    }



    //下载页面
    public function download()
    {
        $this->display();
    }

    //pc端的页面
    public function pc()
    {
        $this->display();
    }

    //手机端的网页
    public function mobile()
    {
        $this->display();
    }

    //添加水印
    public function shuiyin()
    {
        $model = M('images');
        $data = $model->where(1)->select();
        foreach($data as $key=>$value)
        {
            $data[$key]['pic_url'] = $data[$key]['pic_url'].
                '?watermark/1/image/aHR0cDovL29kc2lxam44aS5ia3QuY2xvdWRkbi5jb20vMjAxNi0xMS0wN181ODFmZWE1NTY4ODU5LnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10';
        }
        foreach($data as $key=>$value)
        {
            $condition['id'] = $data[$key]['id'];
            var_dump($condition);
            $update['pic_url'] = $data[$key]['pic_url'];
            $model->where($condition)->save($update);
        }

    }

    //头像水印
    public function touxiang()
    {
        $model = M('userinfo');
        $data = $model->where(1)->field('id,header_pic')->select();
        foreach($data as $key=>$value)
        {
            $data[$key]['header_pic'] = $data[$key]['header_pic'].'?watermark/1/image/aHR0cDovL29kc2lxam44aS5ia3QuY2xvdWRkbi5jb20vMjAxNi0xMS0wN181ODFmZWE1NTY4ODU5LnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10';
        }
        var_dump($data);
        foreach($data as $key=>$value)
        {
            $condition['id'] = $data[$key]['id'];
            $update['header_pic'] = $data[$key]['header_pic'];
            $model->where($condition)->save($update);
        }
    }

    }